package cn.tju.yanbo.commons.anim;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import cn.tju.yanbo.R;
import cn.tju.yanbo.commons.BaseActivity;
import cn.tju.yanbo.commons.db.AppDatabase;
import cn.tju.yanbo.commons.db.DBUtils;
import cn.tju.yanbo.utils.AppConfig;
import cn.tju.yanbo.utils.AppUtils;

import android.app.Activity;
import android.util.Log;
import android.widget.TextView;

public class AnimationController {
    
    private BaseActivity activity = null;
    private Calendar time = Calendar.getInstance();
    private Calendar tmpTime = Calendar.getInstance();
    private TextView dateYearView = null;
    private LinkedList<TextView> dateDayViewList = new LinkedList<TextView>();
    private LinkedList<TextView> dateMonthViewList = new LinkedList<TextView>();
    private LinkedList<TextView> dateHourViewList = new LinkedList<TextView>();
    
    public AnimationController(BaseActivity activity) {
        this.activity = activity;
        
        dateYearView = (TextView)activity.findViewById(R.id.date_year);
        time.set(Calendar.YEAR, Integer.parseInt(dateYearView.getText().toString()));
        
        for(int i=0; i<AppConfig.DATE_VIEW_ID[1].length; i++) {
            TextView view = (TextView)activity.findViewById(AppConfig.DATE_VIEW_ID[1][i]);
            dateMonthViewList.add(view);
            if(AppUtils.toString(view.getTag()).indexOf("month_mid")>=0) {
                time.set(Calendar.MONTH, Integer.parseInt(view.getText().toString()));
            }
        }
        
        if(activity.getRevolveRadius().length>=3) {
            for(int i=0; i<AppConfig.DATE_VIEW_ID[2].length; i++) {
                TextView view = (TextView)activity.findViewById(AppConfig.DATE_VIEW_ID[2][i]);
                dateDayViewList.add(view);
                if(AppUtils.toString(view.getTag()).indexOf("day_mid")>=0) {
                    time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(view.getText().toString()));
                }
            }
        }
        
        if(activity.getRevolveRadius().length==4) {
            Log.i("revolve", "length====="+activity.getRevolveRadius().length);
            for(int i=0; i<AppConfig.DATE_VIEW_ID[3].length; i++) {
                TextView view = (TextView)activity.findViewById(AppConfig.DATE_VIEW_ID[3][i]);
                dateHourViewList.add(view);
                if(AppUtils.toString(view.getTag()).indexOf("hour_mid")>=0) {
                    time.set(Calendar.HOUR_OF_DAY, AppUtils.sinarHour(view.getText().toString()));
                }
            }
        }
    }
    
    public void roll(int field, int value) {
        time.roll(field, value);
        Log.i("revolve", "date====="+AppUtils.format("yyyy-MM-dd", time.getTime())+",roll="+value);
        /*
        LinkedList<TextView> list2roll = null;
        switch(field) {
            case Calendar.MONTH : list2roll=dateMonthViewList; break;
            case Calendar.DAY_OF_YEAR : list2roll=dateDayViewList; break;
            default : break; 
        }
        if(list2roll!=null) {
            if(value<0) {///逆时针
                TextView view = list2roll.removeLast();
                Log.i("revolve", "逆时针：view.text="+view.getText());
                list2roll.addFirst(view);
            } else {//顺时针
                TextView view = list2roll.removeFirst();
                Log.i("revolve", "顺时针：view.text="+view.getText());
                list2roll.addLast(view);
            }
        }
        */
        tmpTime.setTime(time.getTime());
        dateYearView.setText(String.valueOf(tmpTime.get(Calendar.YEAR)));
        
        tmpTime.setTime(time.getTime());
        tmpTime.roll(Calendar.MONTH, -dateMonthViewList.size()/2);
        for(TextView view : dateMonthViewList) {
            view.setText(String.valueOf(tmpTime.get(Calendar.MONTH)+1));
            tmpTime.roll(Calendar.MONTH, 1);
        }

        if(activity.getRevolveRadius().length>=3) {
            tmpTime.setTime(time.getTime());
            tmpTime.roll(Calendar.DAY_OF_YEAR, -dateDayViewList.size()/2);
            for(TextView view : dateDayViewList) {
                Log.i("revolve", view.getText().toString()+"==>"+tmpTime.get(Calendar.DAY_OF_MONTH));
                view.setText(String.valueOf(tmpTime.get(Calendar.DAY_OF_MONTH)));
                tmpTime.roll(Calendar.DAY_OF_YEAR, 1);
            }
        }

        if(activity.getRevolveRadius().length==4) {
            Log.i("revolve", "length====="+activity.getRevolveRadius().length);
            tmpTime.setTime(time.getTime());
            tmpTime.roll(Calendar.HOUR_OF_DAY, -dateHourViewList.size()/2);
            for(TextView view : dateHourViewList) {
                Log.i("revolve", view.getText().toString()+"==>"+tmpTime.get(Calendar.HOUR_OF_DAY));
                view.setText(AppUtils.lunarHour(tmpTime.get(Calendar.HOUR_OF_DAY)));
                tmpTime.roll(Calendar.HOUR_OF_DAY, 2);
            }
        }
        activity.update(time);
    }
    
    public Calendar time() {return time;}
}
